package rov.data;


import rov.data.loader.*;
import rov.ui.gfx.Renderable;

import java.lang.reflect.*;
import java.io.*;
import java.util.*;


// factory for providing easy access to 3D models stored in files (CAD models, etc)
public final class ModelLoader {
    private static final Hashtable<String,IModelLoader> sLoaders = new Hashtable<String,IModelLoader>();


    private ModelLoader() {
    }


    public static final void register(String name, IModelLoader loader) {
        sLoaders.put(name, loader);
    }


    public static final void unregister(String name) {
        sLoaders.remove(name);
    }


    public static final Renderable loadFile(File model) throws IOException {
        if (!model.exists()) {
            System.err.println("Error: file does not exists: \"" + model.getAbsolutePath() + "\"");
            return null;
        }

        for (IModelLoader loader : sLoaders.values()) {
            InputStream in = new FileInputStream(model);

            if (!loader.checkModelFormat(in))
                continue;

            in = new FileInputStream(model);
            Renderable data = loader.loadModel(in);

            if (data != null)
                return data;
        }

        return null;
    }
}

